package com.rchat.pocmini.activity.invitefriendtogroup;

import com.aso.common.http.delegate.HttpCallbackImpl;
import com.aso.common.http.utils.HttpUtils;
import com.rchat.pocmini.bean.UserNoInGroupBean;
import com.rchat.pocmini.global.HttpCodeGlobal;
import com.rchat.pocmini.global.HttpNodeGlobal;
import com.rchat.pocmini.global.RequestParam;
import com.rchat.pocmini.global.UrlsGlobal;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ljd on 2017/9/1.
 */

public class InviteFriendToGroupModelImpl implements InviteFriendToGroupContract.Model {
    @Override
    public void queryFriendNotInGroup(String groupId, String userName, HttpCallbackImpl callback) {
        Map<String, String> param = new HashMap<>();
        param.put("groupId", groupId);
        param.put("userName", userName);
        //body = {"code":1028,"data":{"platform":1,"version":"1.0","enterprise":1,"groupId":"C20795","userName":"111110000118"}}
        String body = RequestParam.requestRootParam(HttpCodeGlobal.REQ_CODE_1028, param);
        HttpUtils.postAsyn(UrlsGlobal.getUrl(UrlsGlobal.QUERY_FRIEND_NOT_IN_GROUP), body, callback);
    }

    @Override
    public void inviteFriendToGroup(String groupId, ArrayList<UserNoInGroupBean.DataBean.UsersBean.UserBean> inviteFriends, HttpCallbackImpl callback) {
        try {

            JSONObject users = new JSONObject();
            JSONArray user = new JSONArray();
            for (UserNoInGroupBean.DataBean.UsersBean.UserBean u : inviteFriends) {
                user.put(u.getUserName());
            }
            users.put("user", user);
            JSONObject root = RequestParam.buildGeneralParam(HttpCodeGlobal.REQ_CODE_1024);
            JSONObject data = new JSONObject();
            data.put(HttpNodeGlobal.NodeReqPlatform,RequestParam.REQUEST_PLATFORM);
            data.put(HttpNodeGlobal.NodeReqVersion, HttpCodeGlobal.getUrlVersion(HttpCodeGlobal.REQ_CODE_1223));
            data.put(HttpNodeGlobal.NodeReqEnterprise, RequestParam.REQUEST_ENTERPRISE);
            data.put("groupId", groupId);
            data.put("users", users);
            root.put("data", data);
            //body = {"code":1024,"data":{"platform":1,"version":"1.0","enterprise":1,"groupId":"C20795","users":{"user":["111110000111","111110000088"]}}}
            String body = root.toString();
            HttpUtils.postAsyn(UrlsGlobal.getUrl(UrlsGlobal.INVITE_FRIEND_TO_GRPOUP), body, callback);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
